package com.one.consumer.user.ui.binder;

import android.content.DialogInterface;
import android.support.annotation.NonNull;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Toast;

import com.one.common.config.RouterPath;
import com.one.common.manager.RouterManager;
import com.one.common.model.http.callback.ObserverOnResultListener;
import com.one.common.utils.MyToast;
import com.one.common.utils.Toaster;
import com.one.common.view.base.BaseActivity;
import com.one.common.view.multitytype.adapter.BaseItemBinder;
import com.one.common.view.multitytype.adapter.BaseViewHolderMulti;
import com.one.consumer.R;
import com.one.consumer.main.ui.MainActivity;
import com.one.consumer.user.model.UserModel;
import com.one.consumer.user.model.extra.InvoiceExtra;
import com.one.consumer.user.model.item.LookUpItem;

import java.util.List;

/**
 * author ：zhulunjun
 * email  ：zhulunjun@qq.com
 * date   ：2018/10/28 4:15 PM
 * description ：发票抬头列表
 **/
public class LookUpBinder extends BaseItemBinder<LookUpItem> {

    public LookUpBinder(LookUpListener listener) {
        super(R.layout.item_look_up);
        this.addressClickListener = listener;
    }

    @Override
    protected void bindView(@NonNull BaseViewHolderMulti holder, @NonNull LookUpItem item) {
        if(item.getInvoice_type().equals("1")) {
            holder.setText(R.id.tv_look_up, item.getInvoice_name());
        } else {
            holder.setText(R.id.tv_look_up, item.getInvoice_company_name());
        }


        CheckBox checkBox = holder.getView(R.id.cb_address);
        if (item.getIs_default()) {
            checkBox.setChecked(true);
        } else {
            checkBox.setChecked(false);
        }

        // 编辑
        holder.getView(R.id.tv_edit).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RouterManager.getInstance().go(RouterPath.ADD_LOOK_UP,
                        new InvoiceExtra(InvoiceExtra.UPDATE, item));
            }
        });

        // 删除
        holder.getView(R.id.tv_delete).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               delete(item);
            }
        });

        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked && !item.getIs_default()) {
                    // 选中
                    changeCheck(item);
                }
            }
        });

        holder.getView(R.id.ll_look_up).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(addressClickListener!=null){
                    addressClickListener.onLookUpClick(item);
                }
            }
        });

    }

    /**
     * 切换选中
     * @param item
     */
    private void changeCheck(LookUpItem item){
        new UserModel((BaseActivity) mContext).defaultAddress(item.getInvoice_id() + "",
                new ObserverOnResultListener<Object>() {
            @Override
            public void onResult(Object o) {
                Toaster.showLongToast("修改默认抬头成功！",MyToast.SUCCESS);
                changeUI(item);
            }
        });
    }

    /**
     * 更新显示
     */
    private void changeUI(LookUpItem item){
        List<LookUpItem> datas = (List<LookUpItem>) getAdapter().getItems();
        for (LookUpItem address : datas){
            if(address.getInvoice_id() == item.getInvoice_id()){
                address.setIs_default("1");
            } else {
                address.setIs_default("0");
            }
        }
        getAdapter().notifyDataSetChanged();
    }


    private void delete(LookUpItem item){
        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
        builder.setMessage("确认删除吗？");
        builder.setPositiveButton("确定",new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                doDelete(item);
            }
        });

        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        builder.show();
    }

    private void doDelete(LookUpItem item){
        new UserModel((BaseActivity) mContext).deleteLookUp(item.getInvoice_id() + "",
                new ObserverOnResultListener<Object>() {
                    @Override
                    public void onResult(Object o) {
                        // 删除成功
                        Toaster.showLongToast("删除成功", MyToast.SUCCESS);
                        addressClickListener.onDelete(item);
                    }
                });
    }


    private LookUpListener addressClickListener;
    public interface LookUpListener{
        void onDelete(LookUpItem item);
        void onLookUpClick(LookUpItem item);
    }


}
